package com.rapida.theme;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;

public class ThemeResources {
	private Resources mDefRes;
	private String mThemePackage;
	private Resources mThemeRes;

	public ThemeResources(Context context) {
		mDefRes = context.getResources();
	}

	public void setThemeContext(Context themeContext) {
		mThemeRes = themeContext.getResources();
		mThemePackage = themeContext.getPackageName();
	}

	private int findThemeResId(int id) {
		return mThemeRes.getIdentifier(mDefRes.getResourceEntryName(id), mDefRes.getResourceTypeName(id), mThemePackage);
	}

	public Drawable getDrawable(int id) {
		Drawable draw;
		try {
			draw = mThemeRes.getDrawable(findThemeResId(id));
		} catch (Exception e) {
			draw = mDefRes.getDrawable(id);
		}
		return draw;
	}

	public CharSequence getText(int id) {
		CharSequence text;
		try {
			text = mThemeRes.getText(findThemeResId(id));
		} catch (Exception e) {
			text = mDefRes.getText(id);
		}
		return text;
	}

	public int getColor(int id) {
		int color;
		try {
			color = mThemeRes.getColor(findThemeResId(id));
		} catch (Exception e) {
			color = mDefRes.getColor(id);
		}
		return color;
	}

	public String getString(int id) {
		String text;
		try {
			text = mThemeRes.getString(findThemeResId(id));
		} catch (Exception e) {
			text = mDefRes.getString(id);
		}
		return text;
	}

	public float getDimension(int id) {
		float dimen;
		try {
			dimen = mThemeRes.getDimension(findThemeResId(id));
		} catch (Exception e) {
			dimen = mDefRes.getDimension(id);
		}
		return dimen;
	}

	public int getDimensionPixelOffset(int id) {
		int dimen;
		try {
			dimen = mThemeRes.getDimensionPixelOffset(findThemeResId(id));
		} catch (Exception e) {
			dimen = mDefRes.getDimensionPixelOffset(id);
		}
		return dimen;
	}

	public int getDimensionPixelSize(int id) {
		int dimen;
		try {
			dimen = mThemeRes.getDimensionPixelSize(findThemeResId(id));
		} catch (Exception e) {
			dimen = mDefRes.getDimensionPixelSize(id);
		}
		return dimen;
	}
}
